package com.aop.springbootaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class LoggingAspect {

    // 定义切入点，拦截 com.aop.springbootaop.mapper 包下以 select 开头的方法
    @Pointcut("execution(* com.aop.springbootaop.mapper.*.select*(..))")
    public void mapperSelectMethods() {}

    // 定义切入点，拦截 com.aop.springbootaop.repository 包下以 select 开头的方法
    @Pointcut("execution(* com.aop.springbootaop.repository.*.select*(..))")
    public void repositorySelectMethods() {}
    @Pointcut("execution(* com.aop.springbootaop.repository.*.find*(..))")
    public void repositoryFindMethods() {}

    @Pointcut("execution(* org.springframework.data.mongodb..*.select*(..)) || " +
            "execution(* org.springframework.data.mongodb..*.find*(..)) || " +
            "execution(* org.springframework.data.mongodb..*.query*(..))")
    public void mongoSelectFindQueryMethods() {}

    @Before("mapperSelectMethods()||repositorySelectMethods()||mongoSelectFindQueryMethods()")
    public void logBefore(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("拦截到的方法名称: " + joinPoint.getSignature().getName());
        System.out.println("拦截到的方法参数: ");
        for (Object arg : args) {
            System.out.println(arg);
        }
        System.out.println("args = " + Arrays.toString(args));


    }
}
